package etrgo

import (
	"net/http"
	"reflect"
	"strconv"
)

func init() {
}

func set(field *reflect.Value,fieldValue string){
	switch field.Kind() {
		case reflect.String :
			field.SetString(fieldValue)		
		case reflect.Uint :
			val , _ := strconv.ParseUint(fieldValue,10,0)		
			field.SetUint(val)
		case reflect.Uint32 :
			val , _ := strconv.ParseUint(fieldValue,10,32)		
			field.SetUint(val)
		case reflect.Uint64 :		
			val , _ := strconv.ParseUint(fieldValue,10,64)		
			field.SetUint(val)
		case reflect.Int :
			val , _ := strconv.ParseInt(fieldValue,10,0)		
			field.SetInt(val)
		case reflect.Int32  :
			val , _ := strconv.ParseInt(fieldValue,10,32)		
			field.SetInt(val)
		case reflect.Int64  :	
			val , _ := strconv.ParseInt(fieldValue,10,64)			
			field.SetInt(val)
		case reflect.Bool :
			val , _ := strconv.ParseBool(fieldValue)
			field.SetBool(val)
		case reflect.Float32:
			val , _ := strconv.ParseFloat(fieldValue,32)
			field.SetFloat(val)
		case reflect.Float64:
			val ,_  := strconv.ParseFloat(fieldValue,64)
			field.SetFloat(val)
	}
}

func ParseParams(r *http.Request,data interface{}){
	s := reflect.ValueOf(data).Elem()
	for fieldName, values := range r.Form {
		if len(values) > 0 {
			value := values[0];
			//fmt.Println("form content",fieldName,value,s.Kind().String(), s.String(),data)			
			field := s.FieldByName(fieldName)
			if field.IsValid() && field.CanSet() {
				set(&field,value)
			}
		}
	}
}
